'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    About:
    This is a collection of utility functions for rig control objects.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import os
from PySide import QtGui
import maya.OpenMaya as OpenMaya
import masdevallia.utility.MScriptUtility as MScriptUtility
import masdevallia.modeling.Curves as Curves
import masdevallia.windows.Dialogs as Dialogs
import masdevallia.settings.PathConstants as PathConstants

'''
============================================================
--->>   UTILITY METHODS
============================================================
'''
def saveSelectedToXML():
    '''
    Saves built controls to an XML file. Currently only handles curves.
    '''
        
    curveShapes = 0
    meshShapes = 0
    surfaceShapes = 0
    
    selList = OpenMaya.MSelectionList()
    OpenMaya.MGlobal.getActiveSelectionList( selList )
    
    if selList.length() > 1:
        
        OpenMaya.MGlobal.displayInfo( 'To many objects selected. Please only select one.' )
        
    elif selList.length() == 1:
        
        # Get the MDagPath and MObject for this item.
        dagPath = OpenMaya.MDagPath()
        selList.getDagPath( 0, dagPath )
        
        # We store the MObject now because later on we change the MDagPath to the
        # object's shapes in order to test their types.
        obj = dagPath.node()
        
        # Get the number of shapes for this kTransform.
        numShapesPtr = MScriptUtility.getIntPointer()
        dagPath.numberOfShapesDirectlyBelow( numShapesPtr[0] )
        numShapes = numShapesPtr[1].getUint( numShapesPtr[0] )
        
        for shape_index in xrange( numShapes ):
            
            # Extend to the shape at the index below this kTransform.
            dagPath.extendToShapeDirectlyBelow( shape_index )
            
            # Get the shape's MObject.
            shape = dagPath.node()
            
            if shape.apiType() == OpenMaya.MFn.kNurbsCurve:
                
                curveShapes += 1
                
            elif shape.apiType() == OpenMaya.MFn.kMesh:
                
                # Handle polygon mesh save.
                print 'MESH'
                meshShapes += 1
        
            elif shape.apiType() == OpenMaya.MFn.kNurbsSurface:
                
                # Handle surface save.
                print 'SURFACE'
                surfaceShapes += 1
                
                
        # We do these checks to handle cases where an object is a a hybrid.
        # Example: a surface that also has curve shapes added.
        if curveShapes > 0 and meshShapes == 0 and surfaceShapes == 0:
                
            # Pure curve object. Can have 1 or more curves.
            initialPath = PathConstants.PATH_MODULE_CONTROLS
            save_browser = Dialogs.BrowserWindow( inStartDirectory=initialPath, inFileMode='AnyFile', inFileTypes=['xml'], inAcceptMode='Save' )
            save_browser.openBrowser()
            
            # Prompt for an icon.
            icon_prompt = Dialogs.ConfirmationWindow( inText='Include an icon?', inInformativeText='Browse for an icon?', inRole='Yes' )
            result = icon_prompt.openDialog()

            if result == QtGui.QMessageBox.Yes:
                icon_browser = Dialogs.BrowserWindow( inStartDirectory=PathConstants.PATH_ICONS, inFileMode='AnyFile', inFileTypes=['png'], inAcceptMode='Open' )
                icon_browser.openBrowser()
                base_name = os.path.basename( icon_browser.selectedFiles[0] )
                filename, ext = os.path.splitext( base_name )
                icon_path = filename
                print filename
            else:
                icon_path = None
            
            # Do the save.                
            XMLData = Curves.writeCurveXML( inCurve=obj, inFilePath=save_browser.selectedFiles[0], inIconPath=icon_path )
            
            
            
            
            
            
            
            
            
            
            
            
            